package com.cirkana;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;

import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;

public abstract class ViewerActivity extends Activity {

	private static final String DATA_KEY = "data";

	private static final String PREFS_NAME = "cirkanaViewer";
	private ViewerPreferences preferences;

	protected ViewerPreferences getPreferences() {
		if (this.preferences == null)
			this.preferences = new ViewerPreferences(this.getSharedPreferences(PREFS_NAME, 0));
		return this.preferences;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCurrentActivity(this);
		setResult(RESULT_CANCELED);
	}

	@SuppressWarnings("unchecked")
	protected PassData getRequest(Class type) {
		try {
			return getPassData(getIntent(), type);
		} catch (BinarySerializationException e) {
			showErrorMessage(e);
			finish();
			return null;
		}
	}

	protected void setResponse(int resultCode, PassData data) throws BinarySerializationException {
		Intent result = new Intent();
		setPassData(result, data);
		super.setResult(resultCode, result);
	}

	@SuppressWarnings("unchecked")
	protected PassData getResponse(Intent resultData, Class type) throws BinarySerializationException {
		return getPassData(resultData, type);
	}

	@SuppressWarnings("unchecked")
	protected void startActivityForResult(Class type, int requestCode, PassData passData) throws BinarySerializationException {
		// TODO: cal controlar aquí els errors de serialització (podria haver un
		// event onBinarySerializationException)
		Intent intent = new Intent(this, type);
		setPassData(intent, passData);
		super.startActivityForResult(intent, requestCode);
	}

	protected void showErrorMessage(String title, String message) {
		openMessageBox(title, message);
	}

	protected void showErrorMessage(Exception e) {
		showErrorMessage("Error", e.getMessage());
	}

	protected void openMessageBox(String title, String message) {
		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.show();
	}

	@SuppressWarnings("unchecked")
	private static PassData getPassData(Intent intent, Class type) throws BinarySerializationException {
		return (PassData) BinarySerializationUtil.deserialize(intent.getExtras().getByteArray(DATA_KEY), type);
	}

	private static void setPassData(Intent intent, PassData data) throws BinarySerializationException {
		intent.putExtra(DATA_KEY, BinarySerializationUtil.serialize(data));
	}

	private static Activity currentActivity;

	public static Activity getCurrentActivity() {
		return ViewerActivity.currentActivity;
	}

	public static void setCurrentActivity(Activity currentActivity) {
		ViewerActivity.currentActivity = currentActivity;
	}

	public static String getLiteral(int id) {
		return ViewerActivity.currentActivity.getResources().getString(id);
	}

}
